package com.hiv.controller.admin;

import cn.hutool.core.convert.Convert;
import com.hiv.common.BaseController;
import com.hiv.common.PageInfo;
import com.hiv.common.WebResponse;
import com.hiv.common.exception.BusinessException;
import com.hiv.common.param.AdminAccountAddParam;
import com.hiv.common.param.AdminAccountEditParam;
import com.hiv.common.param.page.PageQueryAdminParam;
import com.hiv.common.response.AdminRoleVO;
import com.hiv.mapper.domain.Admin;
import com.hiv.service.AdminAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhanghong
 */
@Slf4j
@RestController
@RequestMapping("/admin/manage")
@Api(value = "管理员账号管理模块", tags = {"【管理端】管理员账号管理模块"})
public class AdminManageController extends BaseController {

    @Autowired
    private AdminAccountService adminAccountService;

    /**医院超级账户管理*/
    @ApiOperation(value = "【医院超级管理员】新增", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/hospitalSuper/add")
    public WebResponse<Boolean> hospitalSupperAdd(HttpServletRequest request, @RequestBody AdminAccountAddParam param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        param.setOperator(admin.getName());
        adminAccountService.addAdmin(param);
        return new WebResponse<>(true);
    }
    @ApiOperation(value = "【医院超级管理员】编辑", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/hospitalSuper/update")
    public WebResponse<Boolean> hospitalSuperUpdate(HttpServletRequest request, @RequestBody AdminAccountEditParam param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        param.setOperator(admin.getName());
        adminAccountService.updateAdmin(param);
        return new WebResponse<>(true);
    }
    @ApiOperation(value = "【医院超级管理员】更新状态", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/hospitalSuper/updateStatus")
    public WebResponse<Boolean> hospitalSuperUpdateStatus(HttpServletRequest request, @RequestBody Admin param) throws BusinessException {
        validAdminLoginAndGet(request);
        adminAccountService.updateAdminStatus(param);
        return new WebResponse<>(true);
    }
    @ApiOperation(value = "【医院超级管理员】分页查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/hospitalSuper/pageQuery")
    public WebResponse<PageInfo<AdminRoleVO>> hospitalSuperPageQuery(HttpServletRequest request, @RequestBody PageQueryAdminParam param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        PageInfo<AdminRoleVO> pageInfo = adminAccountService.superPageQueryAdmin(admin, param);
        return new WebResponse<>(pageInfo);
    }
    @ApiOperation(value = "【医院超级管理员】删除", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long", paramType = "query")})
    @PostMapping(path = "/hospitalSuper/delete")
    public WebResponse<Boolean> hospitalSuperDelete(HttpServletRequest request, @RequestParam String ids) throws BusinessException {
        validAdminLoginAndGet(request);
        String[] strings = Convert.toStrArray(ids);
        adminAccountService.removeAdmin(Long.parseLong(strings[0]));
        return new WebResponse<>(true);
    }


    /**医院子级账户管理*/
    @ApiOperation(value = "【医院子账户】分页查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/hospitalSub/pageQuery")
    public WebResponse<PageInfo<AdminRoleVO>> hospitalSubPageQuery(HttpServletRequest request, @RequestBody PageQueryAdminParam param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        PageInfo<AdminRoleVO> pageInfo = adminAccountService.subPageQueryAdmin(admin, param);
        return new WebResponse<>(pageInfo);
    }
    @ApiOperation(value = "【医院子账户】新增", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/hospitalSub/add")
    public WebResponse<Boolean> addAdmin(HttpServletRequest request, @RequestBody AdminAccountAddParam param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        param.setOperator(admin.getName());
        adminAccountService.addAdmin(param);
        return new WebResponse<>(true);
    }
    @ApiOperation(value = "【医院子账户】编辑", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/hospitalSub/update")
    public WebResponse<Boolean> hospitalSubUpdate(HttpServletRequest request, @RequestBody AdminAccountEditParam param) throws BusinessException {
        Admin admin = validAdminLoginAndGet(request);
        param.setOperator(admin.getName());
        adminAccountService.updateAdmin(param);
        return new WebResponse<>(true);
    }
    @ApiOperation(value = "【医院子账户】更新状态", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping(path = "/hospitalSub/updateStatus")
    public WebResponse<Boolean> hospitalSubUpdateStatus(HttpServletRequest request, @RequestBody Admin param) throws BusinessException {
        validAdminLoginAndGet(request);
        adminAccountService.updateAdminStatus(param);
        return new WebResponse<>(true);
    }
    @ApiOperation(value = "【医院子账户】删除", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "管理员ID", required = true, dataType = "long", paramType = "query")})
    @PostMapping(path = "/hospitalSub/delete")
    public WebResponse<Boolean> hospitalSubDelete(HttpServletRequest request, @RequestParam String ids) throws BusinessException {
        validAdminLoginAndGet(request);
        String[] strings = Convert.toStrArray(ids);
        adminAccountService.removeAdmin(Long.parseLong(strings[0]));
        return new WebResponse<>(true);
    }
}
